<!--
  * 预约记录
  *
  * @Author:    TianYuan
  * @Date:      2025-07-22 16:21:55
  * @Copyright  TianYuan
-->
<template>
  <a-modal
      :title="form.reservationId ? '编辑' : '添加'"
      :width="1000"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
      <a-row>
        <a-col :span="12">
          <a-form-item label="企业名称"  name="enterpriseName">
            <a-input style="width: 100%" v-model:value="form.enterpriseName" placeholder="企业名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="预约类型"  name="reservationType">
            <DictSelect width="100%" v-model:value="form.reservationType" :dict-code="DICT_CODE_ENUM.SUPPLIER_TYPE || 'SUPPLIER_TYPE'" placeholder="预约类型"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="预约人姓名"  name="reservationUser">
            <a-input style="width: 100%" v-model:value="form.reservationUser" placeholder="预约人姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="预约人电话"  name="reservationPhone">
            <a-input style="width: 100%" v-model:value="form.reservationPhone" placeholder="预约人电话" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="预约时间"  name="reservationTime">
            <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.reservationTime" style="width: 100%" placeholder="预约时间" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="附件资料1"  name="reservationFile1">
            <FileUpload
                :defaultFileList="form.reservationFile1"
                :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
                buttonText="上传 附件资料1"
                listType="text"
                @change="e => form.reservationFile1 = e"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="附件资料2"  name="reservationFile2">
            <FileUpload
                :defaultFileList="form.reservationFile2"
                :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
                buttonText="上传 附件资料2"
                listType="text"
                @change="e => form.reservationFile2 = e"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { reservationApi } from '/@/api/business/oa/reservation-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import FileUpload from '/@/components/support/file-upload/index.vue';
  import { FILE_FOLDER_TYPE_ENUM } from '/@/constants/support/file-const';
  import {DICT_CODE_ENUM} from "/@/constants/support/dict-const.js";
  import DictSelect from "/@/components/support/dict-select/index.vue";

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      enterpriseName: undefined, //企业名称
      reservationType: undefined, //预约类型
      reservationUser: undefined, //预约人姓名
      reservationPhone: undefined, //预约人电话
      reservationTime: undefined, //预约时间
      reservationFile1: undefined, //附件资料1
      reservationFile2: undefined, //附件资料2
  };

  let form = reactive({ ...formDefault });

  const rules = {
      enterpriseName: [{ required: true, message: '企业名称 必填' }],
      reservationType: [{ required: true, message: '预约类型 必填' }],
      reservationUser: [{ required: true, message: '预约人姓名 必填' }],
      reservationPhone: [{ required: true, message: '预约人电话 必填' }],
      reservationTime: [{ required: true, message: '预约时间 必填' }],
      // reservationFile1: [{ required: true, message: '附件资料1 必填' }],
      // reservationFile2: [{ required: true, message: '附件资料2 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.reservationId) {
        await reservationApi.update(form);
      } else {
        await reservationApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
